﻿#include <util/fist-time-inl.h>
#include <gflags/gflags.h>

#include <thread>

#include "svc_box_info_inl.h"
#include "box/svc_global.h"
#include "box/svc_lobby.h"
#include "box/svc_transmit.h"
#include "svc_box.h"

DECLARE_string(svc_name);
DECLARE_int32(svc_type);
DECLARE_int32(svc_id);

std::string __g_service_alias_name__;
int __g_service_type__;
int __g_service_id__;



template <typename TYPE>
CBoxBase* create_box()
{
    return new TYPE();
}

struct Box
{
    BoxType type;
    CBoxBase* (*create)();
};

// 在这里注册服务工厂
Box g_boxFactory[] = {
    {BOX_TYPE_LOBBY, create_box<CBoxLobby>},
    {BOX_TYPE_TRANSMIT, create_box<CBoxTransmit>},
    {BOX_TYPE_GLOBAL, create_box<CBoxGlobal>},
};


bool g_box_need_stop = false;
void box_run()
{
    __g_service_alias_name__ = FLAGS_svc_name;
    __g_service_id__ = FLAGS_svc_id;
    __g_service_type__ = FLAGS_svc_type;

    CBoxBase* box = nullptr;
    for (auto& [type, create] : g_boxFactory) {
        if (type == FLAGS_svc_type) {
            box = create();
            break;
        }
    }
    if (box == nullptr) {
        return;
    }
    if (!box->Init()) {
        return;
    }

    box->Run();
    
    box->Destroy();
}
